let callback = [];
let pending = false;
let timerFunc;

function flush() {
  callback.forEach(cb => cb());
  pending = false;
}

if (Promise) {
  timerFunc = () => {
    Promise.resolve().then(flush)
  }
} else if (MutationObserver) {
  let observe = new MutationObserver();
  let textNode = document.createTextNode(1);
  observe.observe(textNode, { characterData: true }) // 观测文本的内容

  timerFunc = () => {
    textNode.textContent = 2;
    flush();
  }
} else if (setImmediate) {
  timerFunc = () => {
    setImmediate(flush);
  }
}

export function nextTick(cb) {

  callback.push(cb);

  if (!pending) {
    timerFunc();
    pending = true;
  }
}